<?php

namespace App\Http\Controllers\Api;

use App\Models\Shop\Address;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;

class AddressController extends Controller
{
    public function address_list()
    {
        $user = JWTAuth::parseToken()->authenticate();
        $addresses = Address::where('user_id', $user->id)->get();
        return response()->json(compact('addresses'));
    }

    public function address_create(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();
        $pca = explode(" ", $request->pca,2);
//        return $pca[1];

        Address::create([
            'user_id' => $user->id,
            'name' => $request->name,
            'province' => $pca[0],
//            'city' => $pca['city'],
//            'area' => $pca['area'],
            'tel' => $request->tel,
            'detail' => $request->detail,
        ]);
        $addresses = Address::where('user_id', $user->id)->get();
        return response()->json(compact('addresses'));
    }
}
